package day02;
import java.util.Scanner;
public class demo1{
    private static final int MAX_LOGIN_ATTEMPTS = 3;
    private static final String ADMIN_USERNAME = "admin";
    private static final String ADMIN_PASSWORD = "password";
    private static String[] memberNumbers = new String[100];
    private static String[] memberNames = new String[100];
    private static int[] memberPoints = new int[100];
    private static int memberCount = 0;
    public static void main(String[] args) {
        boolean loggedIn = login();
        if (loggedIn) {
            System.out.println("登录成功！");
            showMenu();
        } else {
            System.out.println("登录失败，没有机会再次登录！");
        }
    }
    private static boolean login() {
        Scanner scanner = new Scanner(System.in);
        int loginAttempts = 0;
        while (loginAttempts < MAX_LOGIN_ATTEMPTS) {
            System.out.print("请输入用户名：");
            String username = scanner.nextLine();
            System.out.print("请输入密码：");
            String password = scanner.nextLine();
            if (username.equals(ADMIN_USERNAME) && password.equals(ADMIN_PASSWORD)) {
                return true;
            } else {
                System.out.println("用户名或密码错误，请重新输入！");
                loginAttempts++;
            }
        }
        return false;
    }
    private static void showMenu() {
        Scanner scanner = new Scanner(System.in);
        int choice;
        do {
            System.out.println("请选择操作：");
            System.out.println("1. 添加会员");
            System.out.println("2. 修改会员信息");
            System.out.println("3. 查询会员信息");
            System.out.println("4. 显示全部会员信息");
            System.out.println("5. 退出系统");
            System.out.print("请输入选项：");
            choice = scanner.nextInt();
            scanner.nextLine(); // 读取换行符
            switch (choice) {
                case 1:
                    addMember();
                    break;
                case 2:
                    updateMember();
                    break;
                case 3:
                    searchMember();
                    break;
                case 4:
                    displayAllMembers();
                    break;
                case 5:
                    System.out.println("退出系统");
                    break;
                default:
                    System.out.println("无效的选项，请重新选择！");
            }
        } while (choice != 5);
    }
    private static void addMember() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入会员号：");
        String memberNumber = scanner.nextLine();
        System.out.print("请输入会员名：");
        String memberName = scanner.nextLine();
        System.out.print("请输入用户积分：");
        int memberPoint = scanner.nextInt();
        scanner.nextLine(); // 读取换行符

        memberNumbers[memberCount] = memberNumber;
        memberNames[memberCount] = memberName;
        memberPoints[memberCount] = memberPoint;
        memberCount++;

        System.out.println("会员添加成功！");
    }
    private static void updateMember() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要修改的会员号：");
        String memberNumber = scanner.nextLine();
        int index = findMemberIndex(memberNumber);
        if (index != -1) {
            System.out.print("请输入新的会员名：");
            String newMemberName = scanner.nextLine();
            System.out.print("请输入新的用户积分：");
            int newMemberPoint = scanner.nextInt();
            scanner.nextLine(); // 读取换行符

            memberNames[index] = newMemberName;
            memberPoints[index] = newMemberPoint;

            System.out.println("会员信息修改成功！");
        } else {
            System.out.println("找不到该会员号的会员！");
        }
    }
    private static void searchMember() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入要查询的会员号：");
        String memberNumber = scanner.nextLine();
        int index = findMemberIndex(memberNumber);
        if (index != -1) {
            System.out.println("会员号：" + memberNumbers[index]);
            System.out.println("会员名：" + memberNames[index]);
            System.out.println("用户积分：" + memberPoints[index]);
        } else {
            System.out.println("找不到该会员号的会员！");
        }
    }
    private static void displayAllMembers() {
        if (memberCount == 0) {
            System.out.println("没有会员信息！");
        } else {
            for (int i = 0; i < memberCount; i++) {
                System.out.println("会员号：" + memberNumbers[i]);
                System.out.println("会员名：" + memberNames[i]);
                System.out.println("用户积分：" + memberPoints[i]);
                System.out.println("--");
            }
        }
    }
    private static int findMemberIndex(String memberNumber) {
        for (int i = 0; i < memberCount; i++) {
            if (memberNumbers[i].equals(memberNumber)) {
                return i;
            }
        }
        return -1;
    }
}
